package com.ruoyi.web.controller.gcgl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gcgl.domain.GcProject;
import com.ruoyi.gcgl.domain.SjProjectPeople;
import com.ruoyi.gcgl.service.IGcCustomerService;
import com.ruoyi.gcgl.service.IGcProjectService;
import com.ruoyi.gcgl.service.ISjProjectPeopleService;
import com.ruoyi.system.service.ISysUserService;

/**
 * 项目成员管理Controller
 * 
 * @author ruoyi
 * @date 2025-08-07
 */
@Controller
@RequestMapping("/sj/people")
public class SjProjectPeopleController extends BaseController
{
    private String prefix = "sj/people";

    @Autowired
    private ISjProjectPeopleService sjProjectPeopleService;
    @Autowired
    private IGcCustomerService gcCustomerService;
    @Autowired
    private IGcProjectService gcProjectService;
    @Autowired
    private ISysUserService userService;

    @RequiresPermissions("gcgl:people:view")
    @GetMapping()
    public String people()
    {
        return prefix + "/people";
    }

    /**
     * 查询项目成员管理列表
     */
    @PostMapping("/list/{gcProjectid}")
    @ResponseBody
    public List<SjProjectPeople> list(SjProjectPeople sjProjectPeople,@PathVariable("gcProjectid") Long gcProjectid)
    {
        sjProjectPeople.setDeleteFlag(1);
        sjProjectPeople.setGcProjectid(gcProjectid);
        List<SjProjectPeople> list = sjProjectPeopleService.selectSjProjectPeopleList(sjProjectPeople);
        return list;
    }

    /**
     * 导出项目成员管理列表
     */
    @RequiresPermissions("gcgl:people:export")
    @Log(title = "项目成员管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SjProjectPeople sjProjectPeople)
    {
        List<SjProjectPeople> list = sjProjectPeopleService.selectSjProjectPeopleList(sjProjectPeople);
        ExcelUtil<SjProjectPeople> util = new ExcelUtil<SjProjectPeople>(SjProjectPeople.class);
        return util.exportExcel(list, "项目成员管理数据");
    }

    /**
     * 新增项目成员管理
     */
    @GetMapping("/add/{id}")
    public String add(ModelMap mmap,@PathVariable("id") Long id){
    	GcProject po = gcProjectService.selectGcProjectById(id);
    	mmap.put("gcProject", po);
        return prefix + "/add";
    }

    /**
     * 新增保存项目成员管理
     */
    @Log(title = "项目成员管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SjProjectPeople sjProjectPeople)
    {
    	sjProjectPeople.setDeleteFlag(1);
    	sjProjectPeople.setCreateUser(getLoginName());
    	sjProjectPeople.setCreateAt(new Date());
    	sjProjectPeople.setUpdateUser(getLoginName());
    	sjProjectPeople.setUpdateAt(new Date());
        return toAjax(sjProjectPeopleService.insertSjProjectPeople(sjProjectPeople));
    }

    /**
     * 修改项目成员管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap){
        SjProjectPeople sjProjectPeople = sjProjectPeopleService.selectSjProjectPeopleById(id);
        mmap.put("sjProjectPeople", sjProjectPeople);
        
        GcProject po = gcProjectService.selectGcProjectById(sjProjectPeople.getGcProjectid());
    	mmap.put("gcProject", po);
        return prefix + "/edit";
    }

    /**
     * 修改保存项目成员管理
     */
    @Log(title = "项目成员管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SjProjectPeople sjProjectPeople)
    {
    	sjProjectPeople.setUpdateUser(getLoginName());
    	sjProjectPeople.setUpdateAt(new Date());
        return toAjax(sjProjectPeopleService.updateSjProjectPeople(sjProjectPeople));
    }

    /**
     * 删除项目成员管理
     */
    @Log(title = "项目成员管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sjProjectPeopleService.deleteSjProjectPeopleByIds(ids));
    }
    
    @GetMapping("/p4people/{id}")
    public String p4people(ModelMap mmap,@PathVariable("id") Long id)
    {
    	GcProject po = gcProjectService.selectGcProjectById(id);
    	mmap.put("gcProject", po);
    	
    	//判断是成员，还是管理员
    	List<SysRole> roles = getSysUser().getRoles();
        List<Long> roleIds = new ArrayList<Long>();
        for(SysRole obj:roles){
        	roleIds.add(obj.getRoleId());
        }
        
    	//看个人
        int flag = 0;//成员
    	if(getSysUser().isShowRoleIds(roleIds)){
    		SjProjectPeople sjProjectPeople = new SjProjectPeople();
            sjProjectPeople.setDeleteFlag(1);
            sjProjectPeople.setGcProjectid(id);
            sjProjectPeople.setCode(getUserId().toString());
            List<SjProjectPeople> peoplelis = sjProjectPeopleService.selectSjProjectPeopleList(sjProjectPeople);
            if(peoplelis!=null && peoplelis.size()>0){
            	for(SjProjectPeople po1:peoplelis){
                	if(po1.getpId()==0l){
                		flag = 1;//领导
                		break;
                	}
                }
            }
        }else{
        	flag = 1;//领导
        }
        mmap.put("flag", flag);
        return "sj/project/projectDetailPeople";
    }

    /**
     * 查询项目成员管理列表
     */
    @PostMapping("/p4list")
    @ResponseBody
    public TableDataInfo p4list(SjProjectPeople sjProjectPeople)
    {
        sjProjectPeople.setDeleteFlag(1);
        List<SjProjectPeople> list = sjProjectPeopleService.selectSjProjectPeopleList(sjProjectPeople);
        
        for(SjProjectPeople ms:list){
        	GcProject po = gcProjectService.selectGcProjectById(ms.getGcProjectid());
        	ms.setProjectName(po.getName());
        }
        
        return getDataTable(list);
    }
    
    //------------------分配用户
    /**
     * 选择用户
     */
    @GetMapping("/selectUser")
    public String selectUser(ModelMap mmap){
        return "/sj/people/subUser";
    }
    
    /**
     * 查询未分配用户角色列表
     */
    @PostMapping("/userList")
    @ResponseBody
    public TableDataInfo userList(SysUser user){
        startPage();
        user.setDelFlag("1");
        List<SysUser> list = userService.selectUserList(user);
        return getDataTable(list);
    }
    
    /**
     * 分配用户
     * @param gcProjectid	项目ID
     * @param userIds		用户ID
     * @return
     */
    @Log(title = "分配用户", businessType = BusinessType.INSERT)
    @PostMapping("/fpUser")
    @ResponseBody
    public AjaxResult selectAuthUserAll(Long gcProjectid, String userIds){
    	System.out.println(userIds);
    	System.out.println(gcProjectid);
        return toAjax(1);
    }
    
    /**
     * 新增项目里程碑
     */
    @GetMapping("/add4Cy/{id}")
    public String add4Cy(ModelMap mmap,@PathVariable("id") Long id){
    	
    	SjProjectPeople people = sjProjectPeopleService.selectSjProjectPeopleById(id);
    	GcProject po = gcProjectService.selectGcProjectById(people.getGcProjectid());
    	mmap.put("gcProject", po);
    	mmap.put("pId", people.getId());
        return prefix + "/addCy";
    }
}